package com.dx.mes.service.process.service;

import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_ADD;
import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_QUERY;
import static com.dx.mes.common.bean.def.GenericDef.BANK_IN;
import static com.dx.mes.common.bean.def.GenericDef.EVT_LGON;
import static com.dx.mes.common.bean.def.GenericDef.E_BIS_OPE;
import static com.dx.mes.common.bean.def.GenericDef.E_BIS_PATHI;
import static com.dx.mes.common.bean.def.GenericDef.E_BIS_TOOL_GR;
import static com.dx.mes.common.bean.def.GenericDef.E_FPPMOVEIN_INVALID_BNK_FLG;
import static com.dx.mes.common.bean.def.GenericDef.E_FPPMOVEIN_INVALID_INPUT;
import static com.dx.mes.common.bean.def.GenericDef.E_FPPMOVEIN_PATH_STATUS_INVALIED;
import static com.dx.mes.common.bean.def.GenericDef.E_FPPMOVEIN_PRD_OPE_NOT_MATCH;
import static com.dx.mes.common.bean.def.GenericDef.E_FPPMOVEIN_SHT_STATUS_INVALIED;
import static com.dx.mes.common.bean.def.GenericDef.E_FPPMOVEIN_TOOL_WO_NOT;
import static com.dx.mes.common.bean.def.GenericDef.E_READ_NOT_FOUND;
import static com.dx.mes.common.bean.def.GenericDef.E_RET_PRD_INFO;
import static com.dx.mes.common.bean.def.GenericDef.PATH_IS_FINISH;
import static com.dx.mes.common.bean.def.GenericDef.PROC_HOLD;
import static com.dx.mes.common.bean.def.GenericDef.PROC_IN_PROCESS;
import static com.dx.mes.common.bean.def.GenericDef.PROC_WAIT;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_OK;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_SET_ERROR;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_MESG_OK;
import static com.dx.mes.common.bean.def.GenericDef.TRX_OUT;
import static com.dx.mes.common.bean.def.GenericDef.T_FPPMOVEIN;
import static com.dx.mes.common.bean.def.GenericDef._NORMAL;
import static com.dx.mes.common.bean.def.GenericDef._SPACE;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.LockMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dx.common.table.ppt.Bis_ope;
import com.dx.common.table.ppt.Bis_opeId;
import com.dx.common.table.ppt.Bis_path;
import com.dx.common.table.ppt.Bis_pathId;
import com.dx.common.table.ppt.Bis_path_swh;
import com.dx.common.table.ppt.Bis_pathi;
import com.dx.common.table.ppt.Bis_pathiId;
import com.dx.common.table.ppt.Bis_tool_gr;
import com.dx.common.table.ppt.Bis_tool_grId;
import com.dx.common.table.ppt.Ret_prd_info;
import com.dx.common.table.ppt.Ret_wo;
import com.dx.mes.common.dao.IBisMdlDefDao;
import com.dx.mes.common.dao.IBisOpeDao;
import com.dx.mes.common.dao.IBisPathDao;
import com.dx.mes.common.dao.IBisPathSwhDao;
import com.dx.mes.common.dao.IBisPathiDao;
import com.dx.mes.common.dao.IBisToolDao;
import com.dx.mes.common.dao.IBisToolGrDao;
import com.dx.mes.common.dao.IRetBoxDao;
import com.dx.mes.common.dao.IRetPrdInfoDao;
import com.dx.mes.common.dao.IRetToolStDao;
import com.dx.mes.common.dao.IRetWODao;
import com.dx.mes.common.table.rtnBean.RouteObj;
import com.dx.mes.common.table.rtnBean.SimpleRtnBeen;
import com.dx.mes.common.transcoder.util.AppContext;
import com.dx.mes.common.transcoder.util.JacksonUtil;
import com.dx.mes.common.transcoder.util.LogUtils;
import com.dx.mes.common.transcoder.util.Util;
import com.dx.mes.facade.process.service.IFppmoveinService;
import com.dx.mes.facade.process.tx.Fppmovein.FppmoveinI;
import com.dx.mes.facade.process.tx.Fppmovein.FppmoveinIA;
import com.dx.mes.facade.process.tx.Fppmovein.FppmoveinO;
import com.dx.mes.facade.process.tx.Fppmovein.FppmoveinOA;

@Service("fppmoveinService")
public class FppmoveinService implements IFppmoveinService {
    private LogUtils logUtils;

    @Autowired
    private IBisToolDao bisToolDao;
    @Autowired
    private IRetToolStDao retToolStDao;
    @Autowired
    private IRetBoxDao retBoxDao;
    @Autowired
    private IRetPrdInfoDao retPrdInfoDao;
    @Autowired
    private IBisMdlDefDao bisMdlDefDao;
    @Autowired
    private IBisOpeDao bisOpeDao;
    @Autowired
    private IBisPathDao bisPathDao;
    @Autowired
    private IBisPathiDao bisPathiDao;
    @Autowired
    private IBisToolGrDao bisToolGrDao;
    @Autowired
    private IBisPathSwhDao bisPathSwhDao;
    @Autowired
    private IRetWODao retWODao;

    public String subMainProc(String evt_no, String strInMsg) {
        AppContext.clear();
        AppContext.setCurrServiceName(FppmoveinService.class.getSimpleName());
        AppContext.setCurrEventNumber(evt_no);
        logUtils = new LogUtils(FppmoveinService.class);
        logUtils.info("[InTrx:" + strInMsg + "]");

        String strOutMsg = null;
        FppmoveinI inTrx = new FppmoveinI();
        FppmoveinO outTrx = new FppmoveinO();

        outTrx.setTrx_id(T_FPPMOVEIN);
        outTrx.setType_id(TRX_OUT);
        outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));
        try {
            inTrx = JacksonUtil.fromJson(strInMsg, FppmoveinI.class);
            char action_flg = inTrx.getAction_flg().charAt(0);
            switch (action_flg) {
	            case ACTION_FLG_QUERY:
	                queryByConditionFnc(inTrx, outTrx);
	                break;
                case ACTION_FLG_ADD://main process
                    moveIn(inTrx, outTrx);
                    break;
				case 'F':
					FQCMoveIn(inTrx,outTrx);
					break;
                default:
                    outTrx = errActionFnc();
            }
            if (RETURN_CODE_OK.equals(outTrx.getRtn_code())) {
                retPrdInfoDao.flush();
            } else {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        } catch (Exception ex) {
            outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
            outTrx.setRtn_mesg("程序执行异常");
            logUtils.error(Util.stackTraceToString(ex));
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        } finally {
            strOutMsg = JacksonUtil.toJSONStr(outTrx);
            logUtils.info("[OutTrx:" + strOutMsg + "]");
        }
        return strOutMsg;
    }

    /**
     * 获取move in WIP
     * @param inTrx
     * @param outTrx
     */
    private void queryByConditionFnc(FppmoveinI inTrx, FppmoveinO outTrx) {
        FppmoveinIA iary = inTrx.getIary().get(0);
        String woIdFk = iary.getWo_id_fk();
        String opeIdFk = iary.getOpe_id_fk();
        String prdSeqId = iary.getPrd_seq_id();

        String hql = "From Ret_prd_info where 1=1 and prd_stat='WAIT'";
        if (woIdFk != null && !woIdFk.equals(_SPACE)) {
            hql = hql + " and wo_id_fk = '" + woIdFk + "'";
        }
        if (prdSeqId != null && !prdSeqId.equals(_SPACE)) {
            hql = hql + " and prd_seq_id = '" + prdSeqId + "'";
        }
        if (opeIdFk != null && !opeIdFk.equals(_SPACE)) {
            hql = hql + " and nx_ope_id_fk = '" + opeIdFk + "'";
        }
        List<Ret_prd_info> ret_prd_infos = retPrdInfoDao.find(hql);
        List<FppmoveinOA> oary = new ArrayList<FppmoveinOA>();
        for (Ret_prd_info ret_prd_info : ret_prd_infos) {
            FppmoveinOA fppmoveinOA = new FppmoveinOA();
            fppmoveinOA.setPrd_seq_id(ret_prd_info.getPrd_seq_id());
            fppmoveinOA.setMdl_id_fk(ret_prd_info.getMdl_id_fk());
            fppmoveinOA.setWo_id_fk(ret_prd_info.getWo_id_fk());
            fppmoveinOA.setPrd_stat(ret_prd_info.getPrd_stat());
            fppmoveinOA.setPrd_grade(ret_prd_info.getPrd_grade());
            fppmoveinOA.setSo_id_fk(ret_prd_info.getSo_id_fk());
            fppmoveinOA.setMdl_id_fk(ret_prd_info.getMdl_id_fk());
            fppmoveinOA.setLot_id(ret_prd_info.getLot_id());
            fppmoveinOA.setNx_ope_id_fk(ret_prd_info.getNx_ope_id_fk());
            fppmoveinOA.setNx_tool_id_fk(ret_prd_info.getNx_tool_id_fk());
            oary.add(fppmoveinOA);
        }
        outTrx.setOary(oary);
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
        return;

    }
    
    /**
     * move in
     *
     * @return
     */
    private void moveIn(FppmoveinI inTrx, FppmoveinO outTrx) {
    	SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
    	if(inTrx.getIary().size() == 0){
            outTrx.setRtn_code(E_FPPMOVEIN_INVALID_INPUT + "");
            outTrx.setRtn_mesg("入账信息不正确");
            return;
    	}
    	FppmoveinIA iary = inTrx.getIary().get(0);
    	Ret_prd_info ret_prd_info = retPrdInfoDao.getWithLock(iary.getPrd_seq_id(), LockMode.UPGRADE);
        if (ret_prd_info == null) {
            outTrx.setRtn_code(E_RET_PRD_INFO + E_READ_NOT_FOUND + "");
            outTrx.setRtn_mesg("屏体信息不存在");
            return;
        }
        
        //检查屏体信息
		rtnBeen = checkPrdInfo(iary, ret_prd_info);
		if (rtnBeen.getlRc() != _NORMAL) {
            outTrx.setRtn_code(rtnBeen.getlRc() + "");
            outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
            return;
		}else if(PROC_IN_PROCESS.equals(ret_prd_info.getPrd_stat())){
			outTrx.setRtn_code(RETURN_CODE_OK);
	        outTrx.setRtn_mesg(RETURN_MESG_OK);
	        return;
		}
		
		//检查工艺路线是否HOLD
		Bis_pathiId bis_pathiId = new Bis_pathiId();
    	bis_pathiId.setPath_id(ret_prd_info.getNx_path_id_fk());
		bis_pathiId.setPath_ver(ret_prd_info.getNx_path_ver_fk());
        Bis_pathi bis_pathi = bisPathiDao.get(bis_pathiId);
        if(bis_pathi == null){
            outTrx.setRtn_code(E_BIS_PATHI + E_READ_NOT_FOUND + "");
            outTrx.setRtn_mesg("未找到屏体工艺路线信息" + bis_pathiId.toString());
            return;
        }else if(PROC_HOLD.equals(bis_pathi.getPath_stat())){
            outTrx.setRtn_code(E_FPPMOVEIN_PATH_STATUS_INVALIED + "");
            outTrx.setRtn_mesg("屏体工艺路线" + bis_pathiId.toString() + "状态为[" + PROC_HOLD + "]");
            return;
//        }else if(!PATH_TYPE_MAIN.equals(bis_pathi.getPath_cate())){
//            outTrx.setRtn_code(E_FPPMOVEIN_PATH_CATE_INVALIED + "");
//            outTrx.setRtn_mesg("屏体工艺路线" + bis_pathiId.toString() + "必须为主工艺路线[" + PATH_TYPE_MAIN + "]");
//            return;
        }
        if(iary.getLot_id()!=null){
            ret_prd_info.setLot_id(iary.getLot_id());
        }
        //Set prd info
        //存储pv信息
        ret_prd_info.setPv_ope_id_fk(ret_prd_info.getCr_ope_id_fk());
        ret_prd_info.setPv_ope_no_fk(ret_prd_info.getCr_ope_no_fk());
        ret_prd_info.setPv_ope_ver_fk(ret_prd_info.getCr_ope_ver_fk());
        ret_prd_info.setPv_path_id_fk(ret_prd_info.getCr_path_id_fk());
        ret_prd_info.setPv_path_ver_fk(ret_prd_info.getCr_path_ver_fk());
        ret_prd_info.setPv_tool_id_fk(ret_prd_info.getCr_tool_id_fk());
        ret_prd_info.setPv_toolg_id_fk(ret_prd_info.getCr_toolg_id_fk());
        ret_prd_info.setPv_proc_id_fk(ret_prd_info.getCr_proc_id_fk());
        ret_prd_info.setPv_tool_run_mode(ret_prd_info.getTool_port_id_fk());
        
        //存储cr信息
        ret_prd_info.setCr_ope_id_fk(ret_prd_info.getNx_ope_id_fk());
        ret_prd_info.setCr_ope_no_fk(ret_prd_info.getNx_ope_no_fk());
        ret_prd_info.setCr_ope_ver_fk(ret_prd_info.getNx_ope_ver_fk());
        ret_prd_info.setCr_path_id_fk(ret_prd_info.getNx_path_id_fk());
        ret_prd_info.setCr_path_ver_fk(ret_prd_info.getNx_path_ver_fk());
        ret_prd_info.setCr_tool_id_fk(iary.getTool_id_fk());
        ret_prd_info.setCr_toolg_id_fk(ret_prd_info.getNx_toolg_id_fk());
        ret_prd_info.setCr_proc_id_fk(ret_prd_info.getNx_proc_id_fk());
        ret_prd_info.setCr_tool_run_mode(ret_prd_info.getNx_tool_run_mode());
        ret_prd_info.setTool_id_fk(iary.getTool_id_fk());
        
        //获取下一制程站点信息
        Bis_path nx_bis_path = new Bis_path();
        Bis_ope nx_bis_ope = new Bis_ope();
        RouteObj nxRouteObj = bisPathDao.getNxPath(ret_prd_info.getNx_path_id_fk(),
                ret_prd_info.getNx_path_ver_fk(), ret_prd_info.getNx_ope_no_fk());
        if (_NORMAL == nxRouteObj.getLRc()) {
            logUtils.info("[login_normal:" + ret_prd_info.getPrd_seq_id() + "]");
            nx_bis_path = nxRouteObj.getPath();
            nx_bis_ope = nxRouteObj.getOpe();
            ret_prd_info.setNx_ope_no_fk(nx_bis_path.getId().getCr_ope_no());
            ret_prd_info.setNx_ope_id_fk(nx_bis_path.getCr_ope_id_fk());
            ret_prd_info.setNx_ope_ver_fk(nx_bis_path.getCr_ope_ver_fk());
            ret_prd_info.setNx_path_id_fk(nx_bis_path.getId().getPath_id_fk());
            ret_prd_info.setNx_path_ver_fk(nx_bis_path.getId().getPath_ver_fk());
            ret_prd_info.setNx_proc_id_fk(nx_bis_ope.getProc_id());
            ret_prd_info.setNx_toolg_id_fk(nx_bis_ope.getToolg_id_fk());
            ret_prd_info.setNx_tool_run_mode(nx_bis_ope.getTool_run_mode());
        } else if (PATH_IS_FINISH == nxRouteObj.getLRc()) {
            logUtils.info("[login_finish:" + ret_prd_info.getPrd_seq_id() + "]");
            ret_prd_info.setNx_toolg_id_fk(_SPACE);
            ret_prd_info.setNx_ope_id_fk(_SPACE);
            ret_prd_info.setNx_ope_no_fk(_SPACE);
            ret_prd_info.setNx_ope_ver_fk(_SPACE);
            ret_prd_info.setNx_path_id_fk(_SPACE);
            ret_prd_info.setNx_path_ver_fk(_SPACE);
            ret_prd_info.setNx_proc_id_fk(_SPACE);
            ret_prd_info.setNx_tool_run_mode(_SPACE);
        } else {
            outTrx.setRtn_code(E_BIS_OPE + E_READ_NOT_FOUND + "");
            outTrx.setRtn_mesg("获取下一制程站点错误：" + nxRouteObj.getGszMsgBuf());
            return;
        }
        
        ret_prd_info.setLogon_usr(inTrx.getEvt_usr());
        ret_prd_info.setPv_logon_timestamp(ret_prd_info.getLogon_timestamp());
        ret_prd_info.setLogon_timestamp(Util.getCurrentTimestamp());
        ret_prd_info.setPrd_stat(PROC_IN_PROCESS);
        ret_prd_info.setEvt_usr(inTrx.getEvt_usr());
        ret_prd_info.setEvt_cate(EVT_LGON);
        ret_prd_info.setEvt_timestamp(Util.getCurrentTimestamp());
        retPrdInfoDao.update(ret_prd_info);
        
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
        return;
    }
    
    private void FQCMoveIn(FppmoveinI inTrx, FppmoveinO outTrx) {
        Timestamp current_timestamp = Util.getCurrentTimestamp();
        FppmoveinIA iary = inTrx.getIary().get(0);
        String evt_usr = inTrx.getEvt_usr();
        String lotId = iary.getLot_id();
        String opeIdFk = iary.getOpe_id_fk();
        String toolIdFk = iary.getTool_id_fk();
        String hqlLot = "FROM Ret_prd_info where lot_id = ?";
        List<Ret_prd_info> retPrdInfoList = retPrdInfoDao.list(hqlLot, lotId);
        for(Ret_prd_info ret_prd_info:retPrdInfoList){
            if (ret_prd_info == null) {
                outTrx.setRtn_code(E_FPPMOVEIN_SHT_STATUS_INVALIED + "");
                outTrx.setRtn_mesg("屏体信息不存在");
                return;
            }
            if ("INPR".equals(ret_prd_info.getPrd_stat())) {
                outTrx.setRtn_code(E_FPPMOVEIN_SHT_STATUS_INVALIED + "");
                outTrx.setRtn_mesg("该屏体已经在制程当中");
                return;
            }
            if ("HOLD".equals(ret_prd_info.getPrd_stat())) {
                outTrx.setRtn_code(E_FPPMOVEIN_SHT_STATUS_INVALIED + "");
                outTrx.setRtn_mesg("该屏体已经HOLD");
                return;
            }
            if (!opeIdFk.equals(ret_prd_info.getNx_ope_id_fk())) {
                outTrx.setRtn_code(E_FPPMOVEIN_PRD_OPE_NOT_MATCH + "");
                outTrx.setRtn_mesg("该屏体不在当前站点");
                return;
            }
            if (BANK_IN.equals(ret_prd_info.getBnk_flg())) {
                outTrx.setRtn_code(E_FPPMOVEIN_TOOL_WO_NOT + "");
                outTrx.setRtn_mesg("该屏体在在制仓内，无法入账");
                return;
            }
            //check path hold
            String wo_id = ret_prd_info.getWo_id_fk();
            Ret_wo ret_wo = retWODao.get(wo_id);
            if(null != ret_wo) {
                String path_id = ret_wo.getPath_id_fk();
                String hql1 = "From Bis_pathi where path_id = '" + path_id + "'";
                List<Bis_pathi> bis_pathis = bisPathiDao.find(hql1);
                if (bis_pathis.size() > 0) {
                    String path_stat = bis_pathis.get(0).getPath_stat();
                    if ("HOLD".equals(path_stat)) {
                        outTrx.setRtn_code(E_FPPMOVEIN_SHT_STATUS_INVALIED + "");
                        outTrx.setRtn_mesg("该订单的工艺路线已经HOLD");
                        return;
                    }
                }
            }
            //存储pv信息
            ret_prd_info.setPv_ope_id_fk(ret_prd_info.getCr_ope_id_fk());
            ret_prd_info.setPv_ope_no_fk(ret_prd_info.getCr_ope_no_fk());
            ret_prd_info.setPv_ope_ver_fk(ret_prd_info.getCr_ope_ver_fk());
            ret_prd_info.setPv_path_id_fk(ret_prd_info.getCr_path_id_fk());
            ret_prd_info.setPv_path_ver_fk(ret_prd_info.getCr_path_ver_fk());
            ret_prd_info.setPv_tool_id_fk(ret_prd_info.getCr_tool_id_fk());
            ret_prd_info.setPv_toolg_id_fk(ret_prd_info.getCr_toolg_id_fk());
            ret_prd_info.setPv_proc_id_fk(ret_prd_info.getCr_proc_id_fk());
            ret_prd_info.setPv_tool_run_mode(ret_prd_info.getTool_port_id_fk());
            ret_prd_info.setExt_3(_SPACE);//清空被批退或者换片的电检和外观检的数据信息
            ret_prd_info.setExt_4(_SPACE);
            ret_prd_info.setExt_5(_SPACE);
            ret_prd_info.setExt_6(_SPACE);

            //存储cr信息
            ret_prd_info.setCr_ope_id_fk(ret_prd_info.getNx_ope_id_fk());
            ret_prd_info.setCr_ope_no_fk(ret_prd_info.getNx_ope_no_fk());
            ret_prd_info.setCr_ope_ver_fk(ret_prd_info.getNx_ope_ver_fk());
            ret_prd_info.setCr_path_id_fk(ret_prd_info.getNx_path_id_fk());
            ret_prd_info.setCr_path_ver_fk(ret_prd_info.getNx_path_ver_fk());
            ret_prd_info.setCr_tool_id_fk(toolIdFk);
            ret_prd_info.setCr_toolg_id_fk(ret_prd_info.getNx_toolg_id_fk());
            ret_prd_info.setCr_proc_id_fk(ret_prd_info.getNx_proc_id_fk());
            ret_prd_info.setCr_tool_run_mode(ret_prd_info.getNx_tool_run_mode());

            ret_prd_info.setTool_id_fk(toolIdFk);
            
            //获取下一制程站点信息
            Bis_path nx_bis_path = new Bis_path();
            Bis_ope nx_bis_ope = new Bis_ope();
            RouteObj nxRouteObj = bisPathDao.getNxPath(ret_prd_info.getNx_path_id_fk(),
                    ret_prd_info.getNx_path_ver_fk(), ret_prd_info.getNx_ope_no_fk());
            if (_NORMAL == nxRouteObj.getLRc()) {
                logUtils.info("[login_normal:" + ret_prd_info.getPrd_seq_id() + "]");
                nx_bis_path = nxRouteObj.getPath();
                nx_bis_ope = nxRouteObj.getOpe();
                ret_prd_info.setNx_ope_no_fk(nx_bis_path.getId().getCr_ope_no());
                ret_prd_info.setNx_ope_id_fk(nx_bis_path.getCr_ope_id_fk());
                ret_prd_info.setNx_ope_ver_fk(nx_bis_path.getCr_ope_ver_fk());
                ret_prd_info.setNx_path_id_fk(nx_bis_path.getId().getPath_id_fk());
                ret_prd_info.setNx_path_ver_fk(nx_bis_path.getId().getPath_ver_fk());
                ret_prd_info.setNx_proc_id_fk(nx_bis_ope.getProc_id());
                ret_prd_info.setNx_toolg_id_fk(nx_bis_ope.getToolg_id_fk());
                ret_prd_info.setNx_tool_run_mode(nx_bis_ope.getTool_run_mode());
            } else if (PATH_IS_FINISH == nxRouteObj.getLRc()) {
                logUtils.info("[login_finish:" + ret_prd_info.getPrd_seq_id() + "]");
                Bis_pathiId bis_pathiIdCR = new Bis_pathiId();
                bis_pathiIdCR.setPath_id(ret_prd_info.getCr_path_id_fk());
                bis_pathiIdCR.setPath_ver(ret_prd_info.getCr_path_ver_fk());
                Bis_pathi bis_pathiCR = bisPathiDao.get(bis_pathiIdCR);
                if (bis_pathiCR.getPath_cate().equals("RWK")) {
                    String hql = "FROM Bis_path_swh where 1=1 ";
                    if (ret_prd_info.getSv_path_id_fk() != null) {
                        hql = hql + " and fr_path_id_fk ='" + ret_prd_info.getSv_path_id_fk() + "'";
                    }
                    if (ret_prd_info.getSv_path_ver_fk() != null) {
                        hql = hql + " and fr_path_ver_fk='" + ret_prd_info.getSv_path_ver_fk() + "'";
                    }
                    if (ret_prd_info.getSv_ope_no_fk() != null) {
                        hql = hql + " and fr_ope_no_fk='" + ret_prd_info.getSv_ope_no_fk() + "'";
                    }
                    hql = hql + " and swh_typ='R'";
                    if (ret_prd_info.getCr_path_id_fk() != null) {
                        hql = hql + " and swh_path_id_fk='" + ret_prd_info.getCr_path_id_fk() + "'";
                    }
                    List<Bis_path_swh> bisPathSwhList = bisPathSwhDao.find(hql);
                    if (bisPathSwhList.size() == 0) {
                        outTrx.setRtn_code(E_FPPMOVEIN_SHT_STATUS_INVALIED + "");
                        outTrx.setRtn_mesg("重工途程所绑定的主途程不存在");
                        return;
                    }
                    //获取信息
                    Bis_pathId bis_pathIdMain = new Bis_pathId();
                    bis_pathIdMain.setPath_id_fk(bisPathSwhList.get(0).getId().getFr_path_id_fk());
                    bis_pathIdMain.setPath_ver_fk(bisPathSwhList.get(0).getId().getFr_path_ver_fk());
                    bis_pathIdMain.setCr_ope_no(bisPathSwhList.get(0).getOut_ope_no_fk());
                    Bis_path bis_pathMain = bisPathDao.get(bis_pathIdMain);
                    if(null==bis_pathMain){
                        outTrx.setRtn_code(E_FPPMOVEIN_SHT_STATUS_INVALIED + "");
                        outTrx.setRtn_mesg("主工艺路线["+ bisPathSwhList.get(0).getId().getFr_path_id_fk()+"]没有站点["+
                                bisPathSwhList.get(0).getOut_ope_no_fk()+"]");
                        return;
                    }
                    Bis_opeId bis_opeIdMain = new Bis_opeId();
                    bis_opeIdMain.setOpe_id(bis_pathMain.getCr_ope_id_fk());
                    bis_opeIdMain.setOpe_ver(bis_pathMain.getCr_ope_ver_fk());
                    Bis_ope bis_opeMain = bisOpeDao.get(bis_opeIdMain);
                    if(null==bis_opeMain){
                        outTrx.setRtn_code(E_FPPMOVEIN_SHT_STATUS_INVALIED + "");
                        outTrx.setRtn_mesg("站点信息不存在["+ bis_pathMain.getCr_ope_id_fk()+"]");
                        return;
                    }
                    //赋值
                    ret_prd_info.setNx_path_id_fk(bisPathSwhList.get(0).getId().getFr_path_id_fk());
                    ret_prd_info.setNx_path_ver_fk(bisPathSwhList.get(0).getId().getFr_path_ver_fk());
                    ret_prd_info.setNx_ope_no_fk(bisPathSwhList.get(0).getOut_ope_no_fk());
                    ret_prd_info.setNx_ope_id_fk(bis_opeMain.getId().getOpe_id());
                    ret_prd_info.setNx_ope_ver_fk(bis_opeMain.getId().getOpe_ver());
                    ret_prd_info.setNx_proc_id_fk(bis_opeMain.getProc_id());
                    ret_prd_info.setNx_tool_run_mode(bis_opeMain.getTool_run_mode());
                    ret_prd_info.setNx_toolg_id_fk(bis_opeMain.getToolg_id_fk());
                } else {
                    ret_prd_info.setNx_toolg_id_fk(_SPACE);
                    ret_prd_info.setNx_ope_id_fk(_SPACE);
                    ret_prd_info.setNx_ope_no_fk(_SPACE);
                    ret_prd_info.setNx_ope_ver_fk(_SPACE);
                    ret_prd_info.setNx_path_id_fk(_SPACE);
                    ret_prd_info.setNx_path_ver_fk(_SPACE);
                    ret_prd_info.setNx_proc_id_fk(_SPACE);
                    ret_prd_info.setNx_tool_run_mode(_SPACE);
                }

            } else {
                outTrx.setRtn_code(E_BIS_OPE + E_READ_NOT_FOUND + "");
                outTrx.setRtn_mesg("获取下一制程站点错误：" + nxRouteObj.getGszMsgBuf());
                return;
            }

            ret_prd_info.setLogon_usr(evt_usr);
            ret_prd_info.setPv_logon_timestamp( ret_prd_info.getLogon_timestamp());
            ret_prd_info.setLogon_timestamp(current_timestamp);
            ret_prd_info.setPrd_stat(PROC_IN_PROCESS);
            ret_prd_info.setEvt_usr(evt_usr);
            ret_prd_info.setEvt_cate(EVT_LGON);
            ret_prd_info.setEvt_timestamp(current_timestamp);
            retPrdInfoDao.update(ret_prd_info);
        }
        outTrx.setRtn_code(RETURN_CODE_OK);
        outTrx.setRtn_mesg(RETURN_MESG_OK);
        return;
    }

	private SimpleRtnBeen checkPrdInfo(FppmoveinIA iary, Ret_prd_info ret_prd_info) {
		Bis_opeId bis_opeId = new Bis_opeId();
        if (PROC_IN_PROCESS.equals(ret_prd_info.getPrd_stat())) {
        	bis_opeId.setOpe_id(ret_prd_info.getCr_ope_id_fk());
        	bis_opeId.setOpe_ver(ret_prd_info.getCr_ope_ver_fk());
        }else if(PROC_WAIT.equals(ret_prd_info.getPrd_stat())){
        	bis_opeId.setOpe_id(ret_prd_info.getNx_ope_id_fk());
        	bis_opeId.setOpe_ver(ret_prd_info.getNx_ope_ver_fk());
        }else{
        	return new SimpleRtnBeen(E_FPPMOVEIN_SHT_STATUS_INVALIED, "屏体状态不正确，必须为[" + PROC_IN_PROCESS 
        			+ "]或者[" + PROC_WAIT + "],请确认");
        }

        //获取站点信息
        Bis_ope bis_ope = bisOpeDao.get(bis_opeId);
        if(bis_ope == null){
        	return new SimpleRtnBeen(E_BIS_OPE + E_READ_NOT_FOUND, "未找到屏体站点信息" + bis_opeId.toString());
        }
        //获取设备群组信息
        Bis_tool_grId bis_tool_grId = new Bis_tool_grId();
        bis_tool_grId.setTool_id_fk(iary.getTool_id_fk());
        bis_tool_grId.setToolg_id(bis_ope.getToolg_id_fk());
        Bis_tool_gr bis_tool_gr = bisToolGrDao.get(bis_tool_grId);
        if(bis_tool_gr == null){
        	return new SimpleRtnBeen(E_BIS_TOOL_GR + E_READ_NOT_FOUND, "屏体站点" + bis_opeId.toString() 
        			+ "对应设备组[" + bis_ope.getToolg_id_fk() 
        			+ "]和入账设备[" + iary.getTool_id_fk() + "]无关联，无法入账，请确认");
        }
        
		//在制仓检查
        if (BANK_IN.equals(ret_prd_info.getBnk_flg())) {
            return new SimpleRtnBeen(E_FPPMOVEIN_INVALID_BNK_FLG, "屏体当前在在制品仓，请先从在制品仓转出");
        }
        
		return new SimpleRtnBeen(_NORMAL);
	}
	
    public FppmoveinO errActionFnc() {
        FppmoveinO FppupkclmO = new FppmoveinO();
        FppupkclmO.setTrx_id(T_FPPMOVEIN);
        FppupkclmO.setType_id(TRX_OUT);
        FppupkclmO.setRtn_code(E_FPPMOVEIN_INVALID_INPUT + "");
        FppupkclmO.setRtn_mesg("Invalid Action_flg");
        return FppupkclmO;
    }
}
